function vendor()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=vendor&tmpl=raw";
	var callback = function(){
		if($("#grid_vendor").length > 0)
		{
			var dataSource = $("#grid_vendor").data("kendoGrid").dataSource;
			dataSource.read();
		}	
		
	}
	obj.callback = callback;
	return obj;
}

function create_vendor()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=vendor&layout=create&tmpl=raw";
	var callback = function(){
		if($("#tab_create_vendor").length > 0)
		{
			initControl("#tab_create_vendor");
			initCreateVendor();
		}
	}
	obj.callback = callback;
	return obj;
}

function edit_vendor()
{
	var localStorage = window.localStorage;	
	var item_arr = localStorage.getItem("edit_vendor");
	if(item_arr == null)
	{
		window.location.hash = $hash_url;
		return;
	}
	var item = JSON.parse(item_arr);
	var id = item.id;
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=vendor&layout=create&tmpl=raw&id="+id;
	var callback = function(){
		if($("#tab_create_vendor").length > 0)
		{
			initControl("#tab_create_vendor");
			initCreateVendor();
		}
	}
	obj.callback = callback;
	return obj;
}

function showModalCreateVendor(client_update)
{
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=vendor&layout=create&tmpl=raw&quickcreate=1&client_update="+client_update;
	obj.link = link;
	var callback = function(){
		if($("#form_create_vendor").length > 0)
		{
			initControl("#tab_create_vendor");
			initCreateVendor(client_update);
		}	
	}
	obj.callback = callback;
	showModalLink(obj);
}

function initCreateVendor(client_update)
{
	
	$("#form_create_vendor").validate({  
		errorPlacement: function(error, element) {
			if($(element).hasClass("error"))
			{
				hideLoader($('#form_create_vendor'));
			}
            error.remove();          
        },
        onkeyup:false,
        onfocusout:false,
        rules:{
            ma:{
                required:true,
                remote:{
        			url:"index.php?option=com_ketnoi&task=vendor.checkMaExists&tmpl=raw",
        			data:{
        				ma:function(value){       	
        					return $("#form_create_vendor input[name='ma']").val();
        				},
        				oldval:$("#oldma").val()
        			},
        			beforeSend:function(){
        				showLoader($('#form_create_vendor'));
        			}
        		}
            },
            ten:{
                required:true,
            }    
            
        },
        messages:{
        	ma:{
                required:'Vui lòng nhập mã', 
                remote:'Mã nhà cung cấp này đã tồn tại.Vui lòng nhập mã khác.'
            },
            ten:{
                required:'Vui lòng nhập tên nhà cung cấp'    
            }
        }
    })
    
    $('#form_create_vendor').ajaxForm({
    	beforeSubmit:function(){   		
    		showLoader($('#form_create_vendor'));
    	},
    	success:function(response){
    		var obj = checkJSON(response);
    		if(obj.is_json)
    		{
    			if(!obj.result.success)
        		{
        			var error = obj.result.error;
        			alertify.alert(error);
        		}else
        		{
        			if(typeof(client_update) != 'undefined')
        			{
        				var option = "<option data-ten='"+obj.result.ten+"' data-ma='"+obj.result.ma+"' value='"+obj.result.id+"'>"+obj.result.ma +" - "+ obj.result.ten+"</option>";
        				$("#"+client_update).append(option);
        				$("#"+client_update).trigger("liszt:updated");
        				$('#form_create_vendor').closest("div.modal").find("div.modal-header button.close").trigger("click");
        				alertify.set({ delay: 4000 });
        				alertify.success("Thêm nhà cung cấp thành công");
        			}
        		}
    		}
    		else
    		{
    			var container = getMainContent();
    			container.html(response);
    			disableHashchange();   			
    			window.location.hash = "edit_vendor";
    			initControl("#tab_create_vendor");
    			initCreateVendor();
    			enableHashchange();
    				
    		}
    		hideLoader($('#form_create_vendor'));
    	}
    });
}

function edit_hash_vendor(id)
{
	item = {};
	item.id = id;
	setHash("edit_vendor",item);
	window.location.hash = "edit_vendor";
}

function deleteVendor(id)
{
	alertify.confirm("Bạn muốn xóa nhà cung cấp này?", function (e) {
	    if (e) {
	    	showLoader();
			var link = "index.php?option=com_ketnoi&view=vendor&task=vendor.delete&tmpl=raw&id="+id;
			$.get(link,"",function(response){
				var dataSource = $("#grid_vendor").data("kendoGrid").dataSource;
				dataSource.read();
				hideLoader();		
				alertify.set({ delay: 4000 });
				alertify.success("Xóa thành công");
			})
	    } 
	});
}

